﻿using MediatR;
using ProduceBasicData.Api.Application.Command.Process;
using ProduceBasicData.Domain.FactoryModeling;
using ProduceBasicData.ErrorCode;
using ProduceBasicData.Infrastructure.Interface;

namespace ProduceBasicData.Api.Application.Handler.Process
{
    /// <summary>
    /// 工序维护批量删除处理类
    /// </summary>
    public class DeleteProcessRangeHandler : IRequestHandler<DeleteProcessRangeCommand, ApiResult<int>>
    {
        private readonly IBaseRespository<ProcessModel> processRepo;

        public DeleteProcessRangeHandler(IBaseRespository<ProcessModel> processRepo)
        {
            this.processRepo = processRepo;
        }

        public async Task<ApiResult<int>> Handle(DeleteProcessRangeCommand request, CancellationToken cancellationToken)
        {
            ApiResult<int> res = new ApiResult<int>();
            var processIds = request.Ids.Split(',').Select(int.Parse).ToList();
            var processlist = processRepo.GetAll().Where(x => processIds.Contains(x.Id)).ToList();

            foreach (var item in processlist)
            {
                item.Isdel = true;
            }
            var r = await processRepo.UpdateRange(processlist);
            res.Data = r;
            res.Code = ApiEnum.Success;
            res.Msg = "删除成功";
            return res;
        }
    }
}
